//
//  MyJobManagementViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/21.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "MyJobManagementViewController.h"
#import "MyJobManageTableViewCell.h"
#import "UIButton+zt_adjustImageAndTitle.h"
#import "AddJobViewController.h"
#import "JobDetailViewController.h"
#import "UIButton+EdgeInsets.h"

@interface MyJobManagementViewController ()<UITableViewDelegate, UITableViewDataSource, MyJobManageCellDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *jobArr;

@end

@implementation MyJobManagementViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self getJobInfo];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	// Do any additional setup after loading the view.
	[self createUI];
	[self refreshData];
}

- (void)createUI {
	
	self.navigationItem.title = @"我的岗位";
	self.view.backgroundColor = UISeparatedColorRGB;
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-20-50) style:UITableViewStylePlain];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	[_tableView registerNib:[UINib nibWithNibName:@"MyJobManageTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
	_tableView.backgroundColor = UISeparatedColorRGB;
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.view addSubview:_tableView];
	
	UIButton *createNewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _tableView.height+20, SCREEN_WIDTH, 50)];
	[createNewBtn setBackgroundColor:UIMainColorRGB];
	[createNewBtn addTarget:self action:@selector(addNewJob:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:createNewBtn];
	[self.view bringSubviewToFront:createNewBtn];
	UIButton *placeHolderBtn = [[UIButton alloc] initWithFrame:CGRectMake(SW(2)-SW(4), 0, SW(2), 50)];
	[placeHolderBtn setImage:IMAGE(@"icon_add_resume") forState:UIControlStateNormal];
	[placeHolderBtn setTitle:@"发布新岗位" forState:UIControlStateNormal];
	placeHolderBtn.userInteractionEnabled = NO;
	[placeHolderBtn layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageLeft imageTitlespace:5];
	[createNewBtn addSubview:placeHolderBtn];
}

- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		[weakSelf getJobInfo];
	}];
	
}

//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	MyJobManageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
	cell.title.text = self.jobArr[indexPath.row][@"title"];
	cell.salary.text = self.jobArr[indexPath.row][@"salary"];
	cell.delegate = self;
	cell.positionId = self.jobArr[indexPath.row][@"position_id"];
	NSString *isDefault = self.jobArr[indexPath.row][@"is_default"];
	if ([isDefault isEqualToString:@"1"]) {
		cell.defaultBtn.selected = YES;
	}
	else {
		cell.defaultBtn.selected = NO;
	}
	
	NSString *hasPost = self.jobArr[indexPath.row][@"status"];
	if ([hasPost isEqualToString:@"1"]) {
		cell.postJobBtn.selected = YES;
	}
	else {
		cell.postJobBtn.selected = NO;
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.jobArr count];
	
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 120;
}

- (NSMutableArray *)jobArr
{
	if (!_jobArr) {
		_jobArr = [[NSMutableArray alloc] init];
	}
	return _jobArr;
}

- (void)getJobInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取我的岗位列表" URLString:APIPositionList parameters:nil method:@"POST" callBack:^(id responseObject) {
		
		[self.jobArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			[self.jobArr addObject:dict];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)addNewJob:(UIButton *)sender
{
	 AddJobViewController *addJobVC = [[AddJobViewController alloc] init];
	[self presentViewController:addJobVC animated:YES completion:nil];
}

- (void)setDefaultJob:(UIButton *)button :(NSString *)positionId
{
	[[NetworkTool sharedTool] requestWithName:@"设置默认岗位" URLString:APISetDefaultJob parameters:@{@"position_id":positionId} method:@"POST" callBack:^(id responseObject) {
		[self getJobInfo];
	}];
}

- (void)postJob:(UIButton *)button :(NSString *)positionId
{
	[[NetworkTool sharedTool] requestWithName:@"上架职位" URLString:APIPostJobOperation parameters:@{@"position_id":positionId} method:@"POST" callBack:^(id responseObject) {
		[self getJobInfo];
		//创建一个消息对象
		NSNotification * notice =[NSNotification notificationWithName:@"changePostJob" object:nil userInfo:@{}];
		//发送消息
		[[NSNotificationCenter defaultCenter] postNotification:notice];
	}];
}

- (void)deleteJob:(UIButton *)button :(NSString *)positionId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定删除？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"删除岗位" URLString:APIDeletePosition parameters:@{@"position_id":positionId} method:@"POST" callBack:^(id responseObject) {
			//NSString *message = responseObject[@"data"][@"message"];
			NSString *message = @"删除成功";
			[self presentViewController:[LZCTools alertMessage:message] animated:YES completion:nil];
			[self getJobInfo];
		}];
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)editJob:(UIButton *)button :(NSString *)positionId
{
	AddJobViewController *editVC = [[AddJobViewController alloc] init];
	editVC.positionId = positionId;
	editVC.type = 1;
	[self presentViewController:editVC animated:YES completion:nil];
}

- (void)previewJob:(UIButton *)button :(NSString *)positionId
{
	JobDetailViewController *vc = [[JobDetailViewController alloc] init];
	vc.position_id = positionId;
	vc.shouldHideDeliverBtn = YES;
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
